package main

import (
	"fmt"
	"go-arch/cs/mmap"
	"os"
	"syscall"
)

func main() {

	file, err := os.OpenFile(mmap.FilePath, syscall.O_RDWR, 0644)
	if err != nil {
		panic(err)
	}

	bytes, err := syscall.Mmap(int(file.Fd()), 0, 1<<8, syscall.PROT_WRITE, syscall.MAP_SHARED)
	if err != nil {
		panic(err)
	}

	fmt.Printf("length = %d, content= %s\n", len(bytes), string(bytes))

	// close file
	if err = file.Close(); err != nil {
		panic(err)
	}

	for i, c := range []byte("hello,mmap") {
		bytes[i] = c
	}

	bytes[254] = 'B'

	if err = syscall.Munmap(bytes); err != nil {
		panic(err)
	}
}
